
import basePanleScript from "../../base/basePanleScript";
import gameModel from "./gameModel";
import AudioUtils from "../AudioUtils";

const { ccclass, property } = cc._decorator;

@ccclass
export default class gameScene extends basePanleScript {
    public onLoaded(param: any): void {
        // throw new Error("Method not implemented.");
    }
    public onEnter(param: any): void {
        // throw new Error("Method not implemented.");
    }
    public onRefresh(param: any): void {
        // throw new Error("Method not implemented.");
    }
    private self=this;

   init(prefab){
            this._get_("gameNode").removeAllChildren();
            var o=cc.instantiate(prefab);
            this.self._get_("gameNode").addChild(o);
   }
   handler(data){
       switch(data.type){
           case "gameState":
            this.controlGame(data.content.state)
           break;
           case "jumpScene":
            this.init(gameModel.getInstance().mainScene)
           break;
       }
   }

   controlGame(state){
       switch(state){
            case "over":
            this.scheduleOnce(function(){
                this.init(gameModel.getInstance().overGame);
            },0.5)
            break;
            case "startGame":
            gameModel.getInstance().isGameOver=false
            this.init(gameModel.getInstance().aloneGame);
            break;
       }
   }

   onEnable(){
        gameModel.getInstance();
        if(!gameModel.getInstance().mainScene){
            var self=this;
            cc.loader.loadRes("prefab/aloneScene/mainScene", function (err, prefab) {
                console.error("mainScene加载完成");
                gameModel.getInstance().mainScene=prefab;
                self.init(prefab)
            });
        }else{
            this.init(gameModel.getInstance().mainScene)
        }
        (<any>window).CustomEventManager.bind("gameState",this);
        (<any>window).CustomEventManager.bind("jumpScene",this);
   }
   onDisabel(){
        (<any>window).CustomEventManager.unBind("gameState",this);
        (<any>window).CustomEventManager.unBind("jumpScene",this);
   }
}
